package com.gaga.console.core.service.impl;

import com.gaga.common.enums.DelFlag;
import com.gaga.common.enums.ScriptType;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import com.gaga.console.base.utils.VideoPlayerUtils;
import com.gaga.console.core.entity.BvScriptItem;
import com.gaga.console.core.entity.dto.AudioPlayDto;
import com.gaga.console.core.service.BvScriptItemService;
import com.gaga.console.core.service.MainVideoService;
import com.gaga.metadata.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author liujia
 * @since 2025-04-06
 */
@Slf4j
@Service
public class MainVideoServiceImpl implements MainVideoService {

    @Autowired
    private BvScriptItemService bvScriptItemService;

    @Override
    public void matchMainVideo(AudioPlayDto audioDto) {

        LambdaQueryWrapperExt<BvScriptItem> bvScriptItemQueryWrapper = new LambdaQueryWrapperExt<>();
        bvScriptItemQueryWrapper.eq(BvScriptItem::getScriptType, ScriptType.MAIN_VIDEO.getCode());
        bvScriptItemQueryWrapper.eq(BvScriptItem::getDelFlag, DelFlag.OK.getCode());
        bvScriptItemQueryWrapper.eq(BvScriptItem::getScriptCategoryId, audioDto.getScriptCategoryId());

        List<BvScriptItem> mainVideoList = bvScriptItemService.list(bvScriptItemQueryWrapper);
        if (mainVideoList == null || mainVideoList.isEmpty()) {
            return;
        }

        // 1. 通过playScriptId关联查找
        if (StringUtils.isNotBlank(audioDto.getPlayScriptId())) {
            Optional<BvScriptItem> first = mainVideoList.stream()
                    .filter(mainVideoItem -> StringUtils.isNotBlank(mainVideoItem.getMainVideoLinkItemId()) && mainVideoItem.getMainVideoLinkItemId().contains(audioDto.getPlayScriptId())).findFirst();
            if (first.isPresent()) {
                String scriptContent = first.get().getScriptContent();
                if (StringUtils.isNotBlank(scriptContent)) {
                    //主视频一个记录保存多个视频
                    String[] split = scriptContent.split(",");
                    int i = new Random().nextInt(split.length);
                    VideoPlayerUtils.playVideo(split[i]);
                    return;
                }
            }
        }

        // 通过关键字
        if (StringUtils.isNotBlank(audioDto.getOldText())) {
            Map<String, String> keywordMap = new HashMap<>();
            for (BvScriptItem bvScriptItem : mainVideoList) {
                if (StringUtils.isBlank(bvScriptItem.getMainVideoLinkKeyword())) {
                    continue;
                }
                for (String keyword : bvScriptItem.getMainVideoLinkKeyword().split("|")) {
                    keywordMap.put(keyword, bvScriptItem.getScriptContent());
                }
            }

            if (keywordMap.isEmpty()) {
                return;
            }

            keywordMap.keySet().stream().filter(keyword -> audioDto.getOldText().contains(keyword)).findFirst().ifPresent(keyword -> {
                VideoPlayerUtils.playVideo(keywordMap.get(keyword));
            });
        }
    }
}
